<template>
    <view class="container m-b6 p-bottom">
        <view class="common-header-xian"></view>
        <custom-ul class="addres-list" v-if="list.length">
            <custom-li v-for="(item, index) in list" :key="index">
                <view class="address-header b-f m-top18">
                    <view class="flow-header-left pl-12">
                        <view class="flow-checkout-admin">
                            {{ item.name }}
                            <text>{{ item.phone }}</text>
                        </view>
                        <view class="flow-checkout-address">{{ item.region.province }} {{ item.region.city }} {{ item.region.region }} {{ item.detail }}</view>
                    </view>
                </view>

                <view class="address-list dis-flex b-f">
                    <view class="list address-left">
                        <radio-group @change="setDefault" class="radio-group">
                            <label class="radio">
                                <radio :checked="item.address_id === default_id" color="#ff495e" :value="item.address_id"></radio>
                                <text class="cont" v-if="item.address_id === default_id">默认</text>
                                <text class="cont" v-else>选择</text>
                            </label>
                        </radio-group>
                    </view>
                    <view class="list address-right dis-flex">
                        <view @tap="editAddress" :data-id="item.address_id">
                            <text class="iconfont icon-edit"></text>
                            <text class="title">编辑</text>
                        </view>
                        <view @tap="removeAddress" :data-id="item.address_id">
                            <text class="iconfont icon-lajixiang"></text>
                            <text class="title">删除</text>
                        </view>
                    </view>
                </view>
            </custom-li>
        </custom-ul>
        <view v-else>
            <view class="yoshop-notcont">
                <text class="iconfont icon-wushuju"></text>
                <text class="cont">亲，您还没有收货列表</text>
            </view>
        </view>
        <view class="footer-fixed f-32">
            <view @tap="createAddress" class="order-bt" style="width: 100%">添加新地址</view>
        </view>
    </view>
</template>

<script>
let app = getApp();
export default {
    data() {
        return {
            list: [],
            default_id: null
        };
    },
    onLoad: function (options) {
        // 当前页面参数
        this.options = options;
    },
    onShow: function () {
        // 获取收货地址列表
        this.getAddressList();
    },
    methods: {
        /**
         * 获取收货地址列表
         */
        getAddressList: function () {
            let that = this;
            app.globalData._get('address/lists', {}, function (result) {
                that.setData(result.data);
            });
        },

        /**
         * 添加新地址
         */
        createAddress: function () {
            uni.navigateTo({
                url: './create'
            });
        },

        /**
         * 编辑地址
         */
        editAddress: function (e) {
            uni.navigateTo({
                url: './detail?address_id=' + e.currentTarget.dataset.id
            });
        },

        /**
         * 移除收货地址
         */
        removeAddress: function (e) {
            let that = this;
            let address_id = e.currentTarget.dataset.id;
            uni.showModal({
                title: '提示',
                content: '您确定要移除当前收货地址吗?',
                success: function (o) {
                    if (o.confirm) {
                        app.globalData._post_form(
                            'address/delete',
                            {
                                address_id
                            },
                            function (result) {
                                that.getAddressList();
                            }
                        );
                    }
                }
            });
        },

        /**
         * 设置为默认地址
         */
        setDefault: function (e) {
            let that = this;
            let address_id = e.detail.value;
            that.setData({
                default_id: parseInt(address_id)
            });
            app.globalData._post_form(
                'address/setDefault',
                {
                    address_id
                },
                function (result) {
                    if (that.options.from === 'flow') {
                        uni.navigateBack();
                    }
                }
            );
            return false;
        }
    }
};
</script>
<style>
@import './index.css';
</style>
